package com.atguigu.edu.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: KeywordUtil
 * Package: com.atguigu.edu.realtime.util
 * Description:
 * ik 分词器工具类
 * @Author Mr.2
 * @Create 2023/9/8 19:37
 * @Version 1.0
 */
public class KeywordUtil {
    // For test output -> yes
    public static void main(String[] args) {
        List<String> list = analyze("Apple iPhoneXSMax (A2104) 256GB 深空灰色 移动联通电信4G手机 双卡双待");
        System.out.println(list);
    }


    // 功能: 有点像 hive 中的lateral view explode()
    public static List<String> analyze (String text) {
        // 创建一个容器
        List<String> keywordList = new ArrayList<>();
        // 字符输入流
        StringReader reader = new StringReader(text);
        // 分割器对象
        IKSegmenter ikSegmenter = new IKSegmenter(reader, true);

        try {
            Lexeme lexeme = null;
            // 循环判断, 使用迭代器
            while ((lexeme = ikSegmenter.next()) != null) {

                String keyword = lexeme.getLexemeText();

                keywordList.add(keyword);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return keywordList;
    }


}
